package com.qf.smsplatform.strategy.mq;

import com.qf.smsplatform.common.constants.RabbitMQConstants;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.strategy.service.StrategyExecutor;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @Author: LeeRui
 * 2021/1/27 下午 8:30
 **/
//监听接口模块发送的MQ消息
@Component
public class PreSendQueueListener {

    //注入策略执行者，获取standardSubmit进行策略执行
    @Autowired
    private StrategyExecutor strategyExecutor;

    //监听接口发送消息队列
    @RabbitListener(queues = {RabbitMQConstants.TOPIC_PRE_SEND})
    public void consume(StandardSubmit standardSubmit, Channel channel, Message message) throws IOException {
        //打印获取的MQ消息
        System.out.println(standardSubmit);

        //调用策略执行统一入口，传入standardSubmit进行策略执行
        strategyExecutor.execute(standardSubmit);

        //消费MQ消息并手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
